package demo16_1;

import java.util.Arrays;
import java.util.Stack;




public class MyStack {
    private int[] elem;
    private int usedSize;

    private static final int DEFAULT_CAPACITY = 10;
    public MyStack() {
        this.elem = new int[DEFAULT_CAPACITY];
    }
    public Boolean isFull() {
        if (this.usedSize == this.elem.length) {
            return true;
        }
        return false;
    }

    public void push(int val) {
        if (isFull()) {
            //扩容 2倍
            this.elem = Arrays.copyOf(this.elem,this.elem.length*2);
        }
        this.elem[this.usedSize] = val;
        this.usedSize++;
    }

    public Boolean isEmpty() {
        if (this.usedSize == 0) {
            return true;
        }
        return false;
    }
    public int pop() {
        if (isEmpty()) {
            throw new EmptyException("栈为空");
        }
        int oldVal = this.elem[this.usedSize - 1];
        this.usedSize--;
        return oldVal;
    }
    public int peek() {
        if (isEmpty()) {
            throw new EmptyException("栈为空");
        }
        return this.elem[this.usedSize - 1];
    }

}
